<script setup>
  import {useRouter} from 'vue-router'
  const router = useRouter()
  import { User, Lock,Eleme,Phone,Message } from '@element-plus/icons-vue'
  import {ElNotification} from 'element-plus'
  import {reactive, ref} from "vue";
  import {useStore} from "vuex";
  import store from "@/store/index.js";

  //控制注册与登录表单的显示， 默认显示注册
  const isRegister = ref(false)
  //定义数据模型
  let registerData = reactive({
    userId:'',
    password:'',
    rePassword:'',
    username:'',
    phone:'',
    code:'',
    email:''
  })

  let loginData=reactive({
    userId:'',
    password:''
  })

  //校验密码的函数
  const checkRePassword = (rule,value,callback)=>{
    if(value===''){
      callback(new Error('请再次确认密码'))
    }else if(value !== registerData.value.password){
      callback(new Error('请确保两次输入的密码一致'))
    }else{
      callback()
    }
  }

  //定义表单校验规则
  const rules={
    userId:[
      {required:true,message:'请输入用户名',trigger:'blur'},
      {min:5,max:20,message:'长度应为5~20位非空字符',trigger:'blur'}
    ],
    password:[
      {required:true,message:'请输入密码',trigger:'blur'},
      {min:5,max:20,message:'长度应为5~20位非空字符',trigger:'blur'}
    ],
    rePassword:[
      {validator:checkRePassword,trigger:'blur'}
    ],
    username:[
      {required:true,message:'请输入昵称',trigger:'blur'},
      {min:1,max:20,message:'长度应为1~20位非空字符',trigger:'blur'}
    ],
    phone:[
      {required:true,message:'请输入手机号',trigger:'blur'},
      { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
    ],
    email:[
      {required:true,message:'请输入邮箱',trigger:'blur'},
      { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
    ]
  }
  //登录
  function login(){
    store.dispatch('loginModle/login', {'userId':loginData.userId, 'password':loginData.password})
  }
  //注册
  function register(){

  }

  //清空注册内容
  const clearRegisterData = ()=>{
    registerData.value={
      userId:'',
      password:'',
      rePassword:'',
      username:'',
      phone:'',
      code:'',
      email:''
    }
  }
</script>

<template>
  <div class="container">
    <el-card class="form">
      <!-- 注册表单 -->
      <el-form ref="form" size="large" autocomplete="off" v-if="isRegister" :model="registerData" :rules="rules">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="userId">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.userId"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerData.password"></el-input>
        </el-form-item>
        <el-form-item prop="rePassword">
          <el-input :prefix-icon="Lock" type="password" placeholder="请再次输入密码" v-model="registerData.rePassword"></el-input>
        </el-form-item>
        <el-form-item prop="username">
          <el-input :prefix-icon="Eleme" placeholder="请输入昵称" v-model="registerData.username"></el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input :prefix-icon="Phone" placeholder="请输入手机号" v-model="registerData.phone"></el-input>
          <el-button style="margin-left: auto" @click="getCode">获取验证码</el-button>
        </el-form-item>
        <el-form-item prop="Code">
          <el-input :prefix-icon="Phone" placeholder="请输入验证码" v-model="registerData.code"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input :prefix-icon="Message" placeholder="请输入邮箱" v-model="registerData.email"></el-input>
        </el-form-item>
        <!-- 注册按钮 -->
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="register">
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false;clearRegisterData()">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
      <!-- 登录表单 -->
      <el-form ref="form" size="large" autocomplete="off" v-else :model="loginData" :rules="rules">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="userId">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="loginData.userId"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="loginData.password"></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true;clearRegisterData()">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style lang="scss" >>
.container{
  margin: 0;
  min-height: 100vh;
  background-color: #cce5ff;
  background-image:
      radial-gradient(closest-side,rgba(79, 172, 254, 1),rgba(0, 242, 254, 0)),
      radial-gradient(closest-side,rgba(132, 250, 176, 1),rgba(143, 211, 244, 0)),
      radial-gradient(closest-side,rgba(48, 207, 208, 1),rgba(51, 8, 103, 0)),
      radial-gradient(closest-side,rgba(22, 160, 133, 1),rgba(244, 208, 63, 0)),
      radial-gradient(closest-side,rgba(112, 133, 182, 1),rgba(222, 243, 248, 0));
  background-size:
      130vmax 130vmax,
      80vmax 80vmax,
      90vmax 90vmax,
      110vmax 110vmax,
      90vmax 90vmax;
  background-position:
      -80vmax -80vmax,
      60vmax -30vmax,
      10vmax 10vmax,
      -30vmax -10vmax,
      50vmax 50vmax;
  background-repeat: no-repeat;
  animation: 8s movement linear infinite;

  body::after {
    content: '';
    display: block;
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
  }

  @keyframes movement {
    0%, 100% {
      background-size:
          130vmax 130vmax,
          80vmax 80vmax,
          90vmax 90vmax,
          110vmax 110vmax,
          90vmax 90vmax;
      background-position:
          -80vmax -80vmax,
          60vmax -30vmax,
          10vmax 10vmax,
          -30vmax -10vmax,
          50vmax 50vmax;
    }
    25% {
      background-size:
          100vmax 100vmax,
          90vmax 90vmax,
          100vmax 100vmax,
          90vmax 90vmax,
          60vmax 60vmax;
      background-position:
          -60vmax -90vmax,
          50vmax -40vmax,
          0vmax -20vmax,
          -40vmax -20vmax,
          40vmax 60vmax;
    }
    50% {
      background-size:
          80vmax 80vmax,
          110vmax 110vmax,
          80vmax 80vmax,
          60vmax 60vmax,
          80vmax 80vmax;
      background-position:
          -50vmax -70vmax,
          40vmax -30vmax,
          10vmax 0vmax,
          20vmax 10vmax,
          30vmax 70vmax;
    }
    75% {
      background-size:
          90vmax 90vmax,
          90vmax 90vmax,
          100vmax 100vmax,
          90vmax 90vmax,
          70vmax 70vmax;
      background-position:
          -50vmax -40vmax,
          50vmax -30vmax,
          20vmax 0vmax,
          -10vmax 10vmax,
          40vmax 60vmax;
    }
  }
}
.form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  user-select: none;
  width: 500px;
  z-index: 1000;

  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  .title {
    margin: 0 auto;
  }

  .button {
    width: 100%;
  }

  .flex {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
}
</style>